﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Entity;
using BotonJXC.Util;
using NHibernate;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms.Controls
{
    public partial class PaymentControl : BaseInfoControl
    {
        PaymentBLL _paymentBll = new PaymentBLL();
        string _paymentName;
        public PaymentControl()
        {
            InitializeComponent();
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _paymentName = ktbPaymentName.Text;
            pager1.PageNo = 1;
            LoadData();
        }

        private void PaymentControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<Payment> pageModel = _paymentBll.Find(_paymentName, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvPayment.DataSource = new BindingList<Payment>(pageModel.Results);
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvPayment.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvPayment.Rows)
                {
                    Payment u = r.DataBoundItem as Payment;
                    if (u == null)
                        continue;
                    if (u.PaymentId == 0)
                    {
                        _paymentBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.PaymentId != 0)
                    {
                        _paymentBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<Payment> payments = (IList<Payment>)kdgvPayment.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvPayment.SelectedRows)
                {
                    Payment item = r.DataBoundItem as Payment;
                    if (item != null)
                    {
                        _paymentBll.Delete(item);
                    }
                    payments.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvPayment.Refresh();
        }

        private void kdgvPayment_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvPayment.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvPayment_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvPayment.CurrentCell.ColumnIndex == 1)
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<Payment> itemDatas = (IList<Payment>)kdgvPayment.DataSource;
            itemDatas[kdgvPayment.CurrentCell.RowIndex].Enabled = cbc.SelectedIndex == 0 ? "Y" : "N";
        }

        private void kdgvPayment_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            IList<Payment> _paymentDatas = kdgvPayment.DataSource as IList<Payment>;
            if (e.ColumnIndex == 1 && _paymentDatas != null && _paymentDatas.Count > e.RowIndex)
            {
                Payment payment = _paymentDatas[e.RowIndex];
                e.Value = "Y".Equals(payment.Enabled) ? "是" : "否";
            }
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
